package com.example.lin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.lin.entity.pojo.User;
import com.example.lin.entity.vo.LoginUser;
import com.example.lin.exception.GlobalException;
import com.example.lin.mapper.UserMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsPasswordService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
@Slf4j
@Service
public class UserDetailsServiceImpl implements UserDetailsManager, UserDetailsPasswordService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 修改密码
     * @param user 用户
     * @param newPassword 新密码
     * @return UserDetails
     */
    @Override
    public UserDetails updatePassword(UserDetails user, String newPassword) {
        return null;
    }

    /**
     * 新增用户
     * @param userDetails 用户
     */
    @Override
    public void createUser(UserDetails userDetails) {
        QueryWrapper<User> wrapper = Wrappers.query();
        wrapper.lambda().eq(User::getUsername, userDetails.getUsername());
        if (userMapper.selectCount(wrapper) > 0) {
            throw new RuntimeException("用户已存在，请更换用户名！");
        }
        User user = new User();
        user.setUsername(userDetails.getUsername()).setPassword(userDetails.getPassword());
        userMapper.insert(user);
    }

    /**
     * 修改用户
     * @param user 用户
     */
    @Override
    public void updateUser(UserDetails user) {

    }

    /**
     * 删除用户
     * @param username 用户名
     */
    @Override
    public void deleteUser(String username) {

    }

    /**
     * 修改密码
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     */
    @Override
    public void changePassword(String oldPassword, String newPassword) {

    }

    /**
     * 判断用户是否存在
     * @param username 用户名
     * @return Boolean
     */
    @Override
    public boolean userExists(String username) {
        QueryWrapper<User> wrapper = Wrappers.query();
        wrapper.lambda().eq(User::getUsername, username);
        return userMapper.selectCount(wrapper) > 0;
    }

    /**
     * 根据用户名查询用户
     * @param username 用户名
     * @return UserDetails
     * @throws UsernameNotFoundException 用户不存在
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<User> wrapper = Wrappers.query();
        wrapper.lambda().eq(User::getUsername, username);
        User user = userMapper.selectOne(wrapper);
        if (ObjectUtils.isEmpty(user)) {
            throw new GlobalException("用户不存在！");
        }
        log.info("用户：{}", user.toString());
        return new LoginUser(user);
    }
}
